package com.jeremyfeinstein.socket;


import com.google.gson.GsonBuilder;
import com.jeremyfeinstein.conference.YCHSConferenceClient;
import com.jeremyfeinstein.gson.ChatMessage;
import com.jeremyfeinstein.utils.ConfigUtil;
import com.jeremyfeinstein.utils.YCHSInstance;
import com.jeremyfeinstein.utils.YCHSWebSocketAPI;

import org.json.JSONObject;

/**
 * Created by sunxunchao on 2019/1/14.
 */
public class SocketHelper {

    private static final String TAG = "SocketHelper";

    /**
     * 获取参会人员列表
     *
     * @param meetingId
     */
    public static void getMeetingUserList(String meetingId) {
        try {
            JSONObject jobj = new JSONObject();
            jobj.put("type", SocketType.TYPE_GetMeetingUserList);
            jobj.put("meetingId", meetingId);
            jobj.put("sessionid", ConfigUtil.getInstance().getSessionId());
            SocketFactory.newYCHSSocket().sendMsg(jobj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送打开和关闭视频流
     *
     * @param action
     */
    public static void contrlMixVideo(String action) {
        try {
            String userCode = YCHSInstance.getInstance().getLoginUser().getUSER_CODE();
            JSONObject jobj = new JSONObject();
            jobj.put("type", SocketType.TYPE_ShowMixVideo);
            jobj.put("meetingId", YCHSConferenceClient.getInstance().getMeetingId());
            jobj.put("userCode", userCode);
            jobj.put("action", action);
            jobj.put("controlUserCode", userCode);
            SocketFactory.newYCHSSocket().sendMsg(jobj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取参会人员列表Alone
     *
     * @param meetingId
     */
    public static void getMeetingUserListAlone(String meetingId) {
        try {
            JSONObject jobj = new JSONObject();
            jobj.put("type", SocketType.TYPE_GetMeetingUserListAlone);
            jobj.put("meetingId", meetingId);
            jobj.put("sessionid", ConfigUtil.getInstance().getSessionId());
            SocketFactory.newYCHSSocket().sendMsg(jobj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改参会人员视频顶部的别名
     *
     * @param meetingId
     */
    public static void editAliasName(String meetingId, String userCode, String aliasName) {
        try {
            JSONObject jobj = new JSONObject();
            jobj.put("type", SocketType.TYPE_EditAliasName);
            jobj.put("meetingId", meetingId);
            jobj.put("userCode", userCode);
            jobj.put("aliasName", aliasName);
            jobj.put("sessionid", ConfigUtil.getInstance().getSessionId());
            SocketFactory.newYCHSSocket().sendMsg(jobj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送布控球控制广播
     *
     * @param status
     * @param action
     */
    public static void controlDevice(String status, String action, String userCode) {
        try {
            JSONObject jobj = new JSONObject();
            jobj.put("type", SocketType.TYPE_CtrlDevice);
            jobj.put("action", status);
            jobj.put("event", action);
            jobj.put("userCode", userCode);
            SocketFactory.newYCHSSocket().sendMsg(jobj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * 加入互动
     */
    public static void join(String meetingId, String streamId, String role, String falg) {
        try {
            JSONObject jobj = new JSONObject();
            jobj.put("type", SocketType.TYPE_Join);
            jobj.put("meetingId", meetingId);
            if ("1".equals(falg)) {
                jobj.put("mobileId", "");
            } else if ("2".equals(falg)) {
                jobj.put("mobileId", ConfigUtil.getInstance().getMobileId());
            }
            jobj.put("streamId", streamId);
            jobj.put("userCode", YCHSInstance.getInstance().getLoginUser()
                    .getUSER_CODE());
            jobj.put("role", role);
            jobj.put("userName", "U"
                    + YCHSInstance.getInstance().getLoginUser().getUSER_CODE());
            jobj.put("sessionid", ConfigUtil.getInstance().getSessionId());
            SocketFactory.newYCHSSocket().sendMsg(jobj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * 申请主讲
     */
    public static void applyForSpeaker(String meetingId, String userCode) {
        try {
            JSONObject jobj = new JSONObject();
            jobj.put("type", SocketType.TYPE_ApplyForSpeaker);
            jobj.put("meetingId", meetingId);
            jobj.put("userCode", userCode);
            jobj.put("sessionid", ConfigUtil.getInstance().getSessionId());
            SocketFactory.newYCHSSocket().sendMsg(jobj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * 互动讨论
     */
    public static void chatMessage(ChatMessage chatMessage) {
        try {
            String chatString = new GsonBuilder().create().toJson(chatMessage);
            JSONObject jobj = new JSONObject(chatString);
            jobj.put("type", SocketType.TYPE_ChatMessage);
            jobj.put("sessionid", ConfigUtil.getInstance().getSessionId());
            SocketFactory.newYCHSSocket().sendMsg(jobj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * 设置参会人员角色
     */
    public static void changeUserRole(String meetingId, String userCode,
                               String roleCode) {
        try {
            JSONObject jobj = new JSONObject();
            jobj.put("type", SocketType.TYPE_ChangeUserRole);
            jobj.put("meetingId", meetingId);
            jobj.put("userCode", userCode);
            jobj.put("roleCode", roleCode);
            jobj.put("sessionid", ConfigUtil.getInstance().getSessionId());
            SocketFactory.newYCHSSocket().sendMsg(jobj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * 申请主讲审批
     */
    public static void approvalForSpeaker(String meetingId, String userCode,
                                   String result) {
        try {
            JSONObject jobj = new JSONObject();
            jobj.put("type", SocketType.TYPE_ApprovalForSpeaker);
            jobj.put("meetingId", meetingId);
            jobj.put("userCode", userCode);
            jobj.put("result", result);
            jobj.put("sessionid", ConfigUtil.getInstance().getSessionId());
            SocketFactory.newYCHSSocket().sendMsg(jobj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * 请出互动室
     */
    public static void dismiss(String meetingId, String userCode) {
        try {
            JSONObject jobj = new JSONObject();
            jobj.put("type", SocketType.TYPE_Dismiss);
            jobj.put("meetingId", meetingId);
            jobj.put("userCode", userCode);
            jobj.put("sessionid", ConfigUtil.getInstance().getSessionId());
            SocketFactory.newYCHSSocket().sendMsg(jobj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * 互动模式设定
     */
    public static void meetingModeSetting(String meetingId, String mode) {
        try {
            JSONObject jobj = new JSONObject();
            jobj.put("type", SocketType.TYPE_MeetingModeSetting);
            jobj.put("meetingId", meetingId);
            jobj.put("mode", mode);
            jobj.put("sessionid", ConfigUtil.getInstance().getSessionId());
            SocketFactory.newYCHSSocket().sendMsg(jobj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * 锁定互动室
     */
    public static void lockMeetingRoom(String meetingId, String lock) {
        try {
            JSONObject jobj = new JSONObject();
            jobj.put("type", SocketType.TYPE_LockMeetingRoom);
            jobj.put("meetingId", meetingId);
            jobj.put("lock", lock);
            jobj.put("sessionid", ConfigUtil.getInstance().getSessionId());
            SocketFactory.newYCHSSocket().sendMsg(jobj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * 关闭用户音频
     */
    public static void deleteAudioStream(String meetingId, String userCode) {
        try {
            JSONObject jobj = new JSONObject();
            jobj.put("type", SocketType.TYPE_deleteAudioStream);
            jobj.put("meetingId", meetingId);
            jobj.put("userCode", userCode);
            jobj.put("sessionid", ConfigUtil.getInstance().getSessionId());
            SocketFactory.newYCHSSocket().sendMsg(jobj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * 打开用户音频
     */
    public static void addAudioStream(String meetingId, String userCode) {
        try {
            JSONObject jobj = new JSONObject();
            jobj.put("type", SocketType.TYPE_addAudioStream);
            jobj.put("meetingId", meetingId);
            jobj.put("userCode", userCode);
            jobj.put("sessionid", ConfigUtil.getInstance().getSessionId());
            SocketFactory.newYCHSSocket().sendMsg(jobj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * 关闭用户视频
     */
    public static void deleteVideoStream(String meetingId, String userCode) {
        try {
            JSONObject jobj = new JSONObject();
            jobj.put("type", SocketType.TYPE_deleteVideoStream);
            jobj.put("meetingId", meetingId);
            jobj.put("userCode", userCode);
            jobj.put("sessionid", ConfigUtil.getInstance().getSessionId());
            SocketFactory.newYCHSSocket().sendMsg(jobj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * 打开用户视频
     */
    public static void addVideoStream(String meetingId, String userCode) {
        try {
            JSONObject jobj = new JSONObject();
            jobj.put("type", SocketType.TYPE_addVideoStream);
            jobj.put("meetingId", meetingId);
            jobj.put("userCode", userCode);
            jobj.put("sessionid", ConfigUtil.getInstance().getSessionId());
            SocketFactory.newYCHSSocket().sendMsg(jobj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * 静音
     */
    public static void disableAudio(String streamId) {
        try {
            JSONObject jobj = new JSONObject();
            jobj.put("type", SocketType.TYPE_DisableAudio);
            jobj.put("streamId", streamId);
            jobj.put("sessionid", ConfigUtil.getInstance().getSessionId());
            jobj.put("meetingId", YCHSConferenceClient.getInstance().getMeetingId());
            SocketFactory.newYCHSSocket().sendMsg(jobj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * 关闭互动室
     */
    public static void endMeetingRoom(String meetingId, String flag) {
        try {
            JSONObject jobj = new JSONObject();
            jobj.put("type", SocketType.TYPE_EndMeetingRoom);
            jobj.put("meetingId", meetingId);
            jobj.put("flag", flag);
            jobj.put("sessionid", ConfigUtil.getInstance().getSessionId());
            SocketFactory.newYCHSSocket().sendMsg(jobj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * 打开音频
     */
    public static void enableAudio(String streamId) {
        try {
            JSONObject jobj = new JSONObject();
            jobj.put("type", SocketType.TYPE_EnableAudio);
            jobj.put("streamId", streamId);
            jobj.put("sessionid", ConfigUtil.getInstance().getSessionId());
            jobj.put("meetingId", YCHSConferenceClient.getInstance().getMeetingId());

            SocketFactory.newYCHSSocket().sendMsg(jobj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * 更改主持人/助理控制图标
     */
    public static void changeHostAssistList(String meetingId, String status,
                                     String userCode) {
        try {
            JSONObject jobj = new JSONObject();
            jobj.put("type", SocketType.TYPE_changeHostAssistList);
            jobj.put("meetingId", meetingId);
            jobj.put("status", status);
            jobj.put("userCode", userCode);
            jobj.put("sessionid", ConfigUtil.getInstance().getSessionId());
            SocketFactory.newYCHSSocket().sendMsg(jobj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 离开互动
     */
    public static void exitMeeting(String meetingId) {
        try {
            JSONObject jobj = new JSONObject();
            jobj.put("type", SocketType.TYPE_ExitMeeting);
            jobj.put("meetingId", meetingId);
            jobj.put("sessionid", ConfigUtil.getInstance().getSessionId());
            SocketFactory.newYCHSSocket().sendMsg(jobj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 分享互动室
     */
    public static void shareVideo(String streamId, String room, String beShareUserCode) {
        try {
            JSONObject jobj = new JSONObject();
            jobj.put("type", SocketType.TYPE_ShareVideo);
            jobj.put("streamId", streamId);
            jobj.put("room", room);
            jobj.put("sessionid", ConfigUtil.getInstance().getSessionId());
            jobj.put("meetingId", YCHSConferenceClient.getInstance()
                    .getMeetingId());
            jobj.put("beShareUserCode", beShareUserCode);
            SocketFactory.newYCHSSocket().sendMsg(jobj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 停止分享
     */
    public static void stopShare() {
        try {
            JSONObject jobj = new JSONObject();
            jobj.put("type", SocketType.TYPE_StopShare);
            jobj.put("sessionid", ConfigUtil.getInstance().getSessionId());
            SocketFactory.newYCHSSocket().sendMsg(jobj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 上传附件通知
     */
    public static void attacheFile() {
        try {
            JSONObject jobj = new JSONObject();
            jobj.put("type", SocketType.TYPE_AttacheFile);
            jobj.put("sessionid", ConfigUtil.getInstance().getSessionId());
            SocketFactory.newYCHSSocket().sendMsg(jobj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void sendForbidTalkSocket(String type, String userCode,
                                      String sreamId) {
        try {
            JSONObject jobj = new JSONObject();
            jobj.put("type", type);
            jobj.put("meetingId", YCHSConferenceClient.getInstance().getMeetingId());
            jobj.put("userCode", userCode);
            jobj.put("streamId", sreamId);
            jobj.put("controlUserCode", YCHSInstance.getInstance().getLoginUser().getUSER_CODE());
            SocketFactory.newYCHSSocket().sendMsg(jobj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
